package com.higgins.eventtracking.server;

import com.higgins.eventtracking.client.ELearningTrackingService;
import com.higgins.eventtracking.client.IncomeSettingsService;
import com.higgins.eventtracking.client.dto.QuestionDTO;
import com.higgins.eventtracking.server.dataModel.Question;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class ELearningTrackingServiceImpl extends RemoteServiceServlet implements ELearningTrackingService {

	@Override
	public String insertQuestion(QuestionDTO dto)
			throws IllegalArgumentException {
		ELearningTrackingDataService dataService = new ELearningTrackingDataService();
		try	{
			dataService.openPersistenceManager();
			dataService.insertQuestion(dto);
			dataService.closePersistenceManager();
			return "Update was successful for income (" +  dto.getQuestion() + ", " + dto.getAnswer() + ").\n";
		} catch (Exception e)	{
			dataService.closePersistenceManager();
			return "Update was unsuccessful for income.\n";
		}		
	}

	@Override
	public QuestionDTO retrieveQuestion(String category) throws IllegalArgumentException {
		ELearningTrackingDataService dataService = new ELearningTrackingDataService();
		try	{
			dataService.openPersistenceManager();
			Question question = dataService.retrieveQuestion(category);
			return question.toQuestionDTO();
		} catch (Exception e)	{
			System.out.println(e);
			return null;
		}	finally	{
			dataService.closePersistenceManager();
		}
	}
}
